package com.proyecto.robomind.map;

import com.proyecto.robomind.model.MapSquare;
import com.proyecto.robomind.model.Ninja;

public class MapHandler {
	
	public static void removeMap(MapSquare[][] mapSquares, MapSquare map){
		for(int i = 0; i < mapSquares.length; i++){
			for(int c = 0; c < mapSquares[i].length; c++){
				if(mapSquares[i][c].equals(map)){
					mapSquares[i][c].setCharacter("e");
				}
			}
		}
	}
	
	public static MapSquare getNextMap(MapSquare[][] mapSquares, float x, float y, int orientation){
		MapSquare grabbedMap = null;
		for(int i = 0; i < mapSquares.length; i++){
			for(int c = 0; c < mapSquares[i].length; c++){
				if(x>mapSquares[i][c].getX() && x<(mapSquares[i][c].getX()+ mapSquares[i][c].getWidth())){
					if(y > mapSquares[i][c].getY() && y <(mapSquares[i][c].getY()+ mapSquares[i][c].getHeight())){
						switch(orientation){
						case 0:
							if(i+1<mapSquares.length){
								grabbedMap = mapSquares[i+1][c];
								mapSquares[i+1][c] = null;
							}
							break;
						case 1:
							if(c+1< mapSquares[i].length){
								grabbedMap = mapSquares[i][c+1];
								mapSquares[i][c+1] = null;
							}
							break;
						case 2:
							if(c-1>=0){
								grabbedMap = mapSquares[i][c-1];
								mapSquares[i][c-1] = null;
							}
							break;
						case 3:
							if(i-1>=0){
								grabbedMap = mapSquares[i-1][c];
								mapSquares[i-1][c] = null;
							}
							break;
						}
					}
				}
			}
		}
		/*if(grabbedMap!=null){
			if(grabbedMap.getCharacter().equals("e")){
				return null;
			}
		}*/
		return grabbedMap;
	}
	
	
	public static boolean hasOrigin(MapSquare[][] mapSquares){
		for(int i = 0; i < mapSquares.length; i++){
			for(int c = 0; c < mapSquares[i].length; c++){
				if(mapSquares[i][c].getCharacter().equals("o")){
					return true;
				}
			}
		}
		return false;
	}
	
	public static MapSquare atOrigin(MapSquare[][] mapSquares){
		for(int i = 0; i < mapSquares.length; i++){
			for(int c = 0; c < mapSquares[i].length; c++){
				if(mapSquares[i][c].getCharacter().equals("o")){
					return mapSquares[i][c];
				}
			}
		}
		return null;
	}
	
	public static MapSquare getTouchedMapSquare(float x, float y, MapSquare[][] mapSquares){
		for(int i = 0; i < mapSquares.length; i++){
			for(int c = 0; c < mapSquares[i].length; c++){
				if(x>mapSquares[i][c].getX() && x<(mapSquares[i][c].getX()+ mapSquares[i][c].getWidth())){
					if(y > mapSquares[i][c].getY() && y <(mapSquares[i][c].getY()+ mapSquares[i][c].getHeight())){
						return mapSquares[i][c];
					}
				}
			}
		}
		return null;
	}
	
    public static MapSquare[][] concatenizeMap(String map, int height, int width){
    	int widthCount = map.indexOf("*");
    	int heightCount = map.length()/(widthCount+1);
    	MapSquare[][] mapSquares = new MapSquare[heightCount][widthCount];
    	for(int i = 0; i<heightCount; i++){
    		for(int c = 0; c < widthCount; c++){
    			MapSquare ms = new MapSquare(height/heightCount, width/widthCount,c*(width/widthCount), i*(height/heightCount));
    			ms.setCharacter(String.valueOf(map.charAt(c + (i*(widthCount+1)))));
    			mapSquares[i][c] = ms;
    		}
    	}
    	return mapSquares;
    }
    
    public static String unconcatenizeMap(MapSquare[][] mapSquares){
    	String mapString = "";
    	for(int i = 0; i < mapSquares.length; i++){
    		for(int c = 0; c < mapSquares[i].length; c++){
    			mapString += mapSquares[i][c].getCharacter();
    		}
    		mapString += "*";
    	}
    	return mapString;
    }
}
